function HandleTableProducePurchases(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  
  this._user = (options && options.user) ? options.user : null;
  this._date = (options && options.date) ? options.date : null;
  this._rest = (options && options.rest) ? options.rest : [];
  this._produces = (options && options.produces) ? options.produces : [];
  this._purchases = (options && options.purchases) ? options.purchases : [];
  this._parent = (options && options.parent) ? options.parent : null;
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : null;
  
  this._createElements();
};

HandleTableProducePurchases.prototype._createElements = function() {
  var tr, td, a, _self = this;
  
  if (this._item.length == 0 && this._rest.length == 0) {
    tr = this._gui.table.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 6;
    td.style.height = '40px';
    if ( this._parent.disposalstatus  == 1 || this._parent.finished == 1) {
      td.style.backgroundColor = '#ccc';
      td.style.borderRight = 'solid 1px #999';
      td.style.borderBottom = 'solid 1px #999';
    } else {
      td.style.backgroundColor = '#fff';
      td.style.borderRight = 'solid 1px #694247';
      td.style.borderBottom = 'solid 1px #694247';
    }
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }

  for (var i = 0, il = this._item.length; i < il; i++) {
    var produce = this._item[i];
    tr = this._gui.table.insertRow(-1);
    new TableRowProducePurchase(tr, produce, this._options);
  }

  /* 显示没有流程的产品 */
  MiscUtils.sort(this._rest, ['name']);
  DlmanageUtils.compare(this._rest, this._item);
  for (var i = 0, il = this._rest.length; i < il; i++) {
    var purchase = this._rest[i];
    if (purchase._added) {
      continue;
    }
    var status = '-';
    if (purchase.cancelled == 0 && purchase.model != '' && purchase.model != Constant.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE && purchase.model != Constant.MODEL_NAME_PRODUCT_VIDEO && purchase.model != Constant.MODEL_NAME_PRODUCT_VIDEODVD && purchase.model != Constant.MODEL_NAME_PRODUCT_VIDEO_BLURAY && purchase.model != Constant.MODEL_NAME_PRODUCT_VIDEODVD_BLURAY) {
      status = 'Waiting for all photo DVD';
      if (purchase.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || purchase.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}') {
        for (var j = 0, jl = this._produces.length; j < jl; j++) {
          var produce = this._produces[j];
          if (produce.pi_trackId == purchase.otherId) {
            status = 'Waiting for ' + produce.title + ' completed';
          }
        }
      }
    } else if (purchase.cancelled ==0 && purchase.model != '' && purchase.model == Constant.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE) {
      status = 'Waiting for photo retouch designed';
    } else if (purchase.cancelled ==0 && purchase.model != '' && purchase.model == Constant.MODEL_NAME_PRODUCT_VIDEO) {
      status = 'Waiting for music, requirement form and tape(s)';
    } else if (purchase.cancelled ==0 && purchase.model != '' && purchase.model == Constant.MODEL_NAME_PRODUCT_VIDEO_BLURAY) {
      status = 'Waiting for music, requirement form and tape(s)';
    } else if (purchase.cancelled ==0 && purchase.model != '' && purchase.model == Constant.MODEL_NAME_PRODUCT_VIDEODVD) {
      status = 'Waiting for video editing completed';
    } else if (purchase.cancelled ==0 && purchase.model != '' && purchase.model == Constant.MODEL_NAME_PRODUCT_VIDEODVD_BLURAY) {
      status = 'Waiting for bluray video editing completed';
    }
    
    tr = this._gui.table.insertRow(-1);
    if (this._parent.disposalstatus == 1 || this._parent.finished == 1) {
      tr.style.backgroundColor = '#ccc';
    } else {
      tr.style.backgroundColor = (tr.rowIndex % 2 != 0) ? '#fff' : '#faf9f4';
    }
    td = tr.insertCell(-1);
    if ( this._parent.disposalstatus == 1 || this._parent.finished == 1) {
      td.style.borderRight = 'solid 1px #999';
      td.style.borderBottom = 'solid 1px #999';
    } else {
      td.style.borderRight = 'solid 1px #694247';
      td.style.borderBottom = 'solid 1px #694247';
    }
    td.align = 'left';
    td.style.height = '24px';
    td.style.padding = '0 0 0 12px';
    if (purchase.urgent == 1 && purchase.cancelled == 0) {
      var img = document.createElement('img');
      img.src = 'images/common/urgented.png';
      td.appendChild(img);
    }
    var span = document.createElement('span');
    span.style.fontWeight = 'bold';
    span.appendChild(document.createTextNode(purchase.quantity + 'X '));
    td.appendChild(span);
    td.appendChild(document.createTextNode(purchase.name));
    
    
    if (purchase.selects.length > 0) {
      a = document.createElement('a');
      a.style.fontSize = '10px';
      a.href = '#';
      a.appendChild(document.createTextNode('Info'));
      a._purchase = purchase;
      a.onclick = function() { var func1 = function() { _self._popupBox2._close(); };
                               var div = document.createElement('div');
                               MiscUtils.dialog(_self._popupBox2, DOMUtils.findPos(this), div, func1, null, {t: this._purchase.name, width: 520, height: 100});
                               new DialogDetails(div, this._purchase, { orderId: (_self._parent) ? _self._parent.id : null, purchases: _self._purchases, hidePrice: _self._options.hidePrice, pur: true });
                               return false;
                             };
      td.appendChild(a);
    }
    
    if (this._parent.disposalstatus == 1 || purchase.cancelled == 1) {
      span.style.textDecoration = 'line-through';
      td.style.textDecoration = 'line-through';
      if (purchase.selects.length > 0) {
        a.style.textDecoration = 'line-through';
      }
    }

    var note = document.createElement('img');
    note.src = (purchase.notes.length != 0) ? 'images/buttons/note_hilight.png' : 'images/buttons/note.png';
    note.style.cursor = 'pointer';
    note.style.margin = '0 0 0 5px';
    note._purchase = purchase;
    note.onclick = function() { var div, pos, func1, func2;
                                div = document.createElement('div');
                                pos = DOMUtils.findPos(this);
                                MiscUtils.dialog(_self._popupBox2, pos, div, null, null, {t: this._purchase.name, width: 760, height: 100});
                                new DialogPurchaseNote(div, this._purchase, { author: _self._user, now: _self._date, popupBox: _self._popupBox2, callbackFunc: function() { _self._callbackFunc();}, parent: _self._parent});
                                return false;
                              };
    td.appendChild(note);
    
    td = tr.insertCell(-1);
    if ( this._parent.disposalstatus == 1 || this._parent.finished == 1) {
      td.style.borderRight = 'solid 1px #999';
      td.style.borderBottom = 'solid 1px #999';
    } else {
      td.style.borderRight = 'solid 1px #694247';
      td.style.borderBottom = 'solid 1px #694247';
    }
    td.align = 'left';
    td.style.height = '24px';
    td.style.padding = '0 0 0 12px';
    
    td = tr.insertCell(-1);
    if ( this._parent.disposalstatus == 1 || this._parent.finished == 1) {
      td.style.borderRight = 'solid 1px #999';
      td.style.borderBottom = 'solid 1px #999';
    } else {
      td.style.borderRight = 'solid 1px #694247';
      td.style.borderBottom = 'solid 1px #694247';
    }
    td.align = 'left';
    td.style.height = '24px';
    td.style.padding = '0 0 0 12px';
    if (this._parent.disposalstatus == 1 || purchase.cancelled == 1) {
      td.appendChild(document.createTextNode('Cancelled'));
    } else if (this._parent.finished == 1) {
      td.appendChild(document.createTextNode('Closed'));
    } else {
      td.appendChild(document.createTextNode(status));
    }
    if (status != '-' && this._parent.finished == 0) {
      a = document.createElement('a');
      a.style.fontSize = '10px';
      a.href = '#';
      a._purchase = purchase;
      a.appendChild(document.createTextNode('Log'));
      a.onclick = function() { var pos = DOMUtils.findPos(this);
                               var func1 = function() { _self._popupBox2._close(); };
                               var div = document.createElement('div');
                               MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, {t: this._purchase.name, width: 820, height: 50});
                               var options = MiscUtils.clone(_self._options);
                               options.isPur = true;
                               new DialogHistory(div, this._purchase, options);
                               return false;
                             };
      td.appendChild(a);
    }
    
    td = tr.insertCell(-1);
    if (this._parent.disposalstatus == 1 || this._parent.finished == 1) {
      td.style.borderRight = 'solid 1px #999';
      td.style.borderBottom = 'solid 1px #999';
    } else {
      td.style.borderRight = 'solid 1px #694247';
      td.style.borderBottom = 'solid 1px #694247';
    }
    td.align = 'center';
    td.style.height = '24px';
    if (this._user.type == '{{$smarty.const.USER_TYPE_MANAGER|escape:javascript}}') {
      if (purchase.cancelled == 0) {
        if (purchase.urgent == 1) {
          var img1 = document.createElement('img');
          img1.src = 'images/common/reverse.png';
          img1.style.cursor = 'pointer';
          img1._purchase = purchase;
          img1.onclick = function() { this._purchase.urgent = 0;
                                      var pos = DOMUtils.findPos(this);
                                      new RequestUtils()._mwrite('purchaseitem', [this._purchase], [], function() { _self._callbackFunc();}, {pos: pos});
                                    };
          td.appendChild(img1);
        } else {
          var img = document.createElement('img');
          img.src = 'images/common/urgent.png';
          img.style.cursor = 'pointer';
          img._purchase = purchase;
          img.onclick = function() { this._purchase.urgent = 1;
                                     var pos = DOMUtils.findPos(this);
                                     new RequestUtils()._mwrite('purchaseitem', [this._purchase], [], function() { _self._callbackFunc();}, {pos: pos});
                                   };
          td.appendChild(img);
        }
      }
    }
    td = tr.insertCell(-1);
    if ( this._parent.disposalstatus == 1 || this._parent.finished == 1) {
      td.style.borderRight = 'solid 1px #999';
      td.style.borderBottom = 'solid 1px #999';
    }else{
      td.style.borderRight = 'solid 1px #694247';
      td.style.borderBottom = 'solid 1px #694247';
    }
    td.align = 'left';
    td.style.height = '24px';
    td.style.padding = '0 0 0 12px';
  }
};
